/**
 * ITLSaasMenuServiceImpl.java com.lianniu.mgr.service.impl
 *
 * Function： TODO
 *
 * ver date author ────────────────────────────────── 0.1 2016年3月24日 王天锦
 *
 * Copyright (c) 2016, TNT All Rights Reserved.
 */

package com.lianniu.mgr.baseService.menu.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.menu.dao.IMenuMapperDao;
import com.lianniu.mgr.baseService.menu.model.MenuSysVo;
import com.lianniu.mgr.baseService.menu.model.MenuVo;
import com.lianniu.mgr.baseService.menu.service.IMenuService;
import com.lianniu.mgr.baseService.post.dao.IPostMapperDao;
import com.lianniu.mgr.baseService.post.model.Post2MenuVo;
import com.lianniu.mgr.baseService.post.model.PostVo;
import com.lianniu.mgr.baseService.staff.dao.IStaffMapperDao;
import com.lianniu.mgr.baseService.staff.model.Staff2PostVo;
import com.lianniu.mgr.baseService.staff.model.StaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

/**
 * 
 * @ClassName：MenuServiceImpl
 * @Description:菜单管理

 * @Since: 2016年4月8日
 * @Version:0.1
 */
@Service
public class MenuServiceImpl implements IMenuService {

  private final static Logger logger = Logger.getLogger(MenuServiceImpl.class);
  @Autowired
  IMenuMapperDao menuMapper;

  @Resource
  IStaffMapperDao staffDao;

  @Resource
  IPostMapperDao postMapperDao;



  /**
   * 
   * getMenuAll:获取所有菜单列表
   * 
   * @param @return 设定文件
   * @return List<MenuVo> DOM对象
   * @throws
   */
  @Override
  public BaseResult getSysInfoMap() {
	  logger.info("getSysInfoMap begin.");
	  
    List<MenuSysVo> List = menuMapper.getSysInfoList();
    logger.info("getSysInfoMap end.");
    return BaseResult.success(List);

  }


  /**
   * 
   * menuAdd:新增菜单
   * 
   * @param @param menuVo
   * @param @return 设定文件
   * @return int DOM对象
   * @throws
   */
  @Override
  public BaseResult addMenu(MenuVo menuVo) {
	  logger.info("getMenuAll begin.");
	  logger.info("menuVo:" + JSON.toJSONString(menuVo));
   /* int sortCount = menuMapper.checkSortNum(menuVo.getParentId(), menuVo.getSortNum(), null);
    if (sortCount == 0) {*/
      int count = menuMapper.addMenu(menuVo);
      logger.info("getMenuAll end.");
      return BaseResult.success(count);
    /*} else {
      return BaseResult.success(-1);// 排序号重复
    }*/
  }
  
  /**
   * @deprecated:填充三级菜单
   */
  void fillMenu(List<MenuVo> mList,List<MenuVo> list){
	  logger.info("fillMenu begin.");
	  for (MenuVo vo : mList) {
		List<MenuVo> tList=new ArrayList<MenuVo>();
		for(int i=0;i<list.size();){
			MenuVo mVo=list.get(i);
			if(vo.getId().equals(mVo.getParentId())){
				tList.add(mVo);
				list.remove(i);//删除已经填充项
			}
			else{
				i++;
			}
		}
		vo.setChild(tList);
	  }
	  logger.info("fillMenu end.");
  }


  /**
   * 根据系统Id获取菜单
   */
  @Override
  public BaseResult getMenuBySysId(String sysId) {
	  logger.info("getMenuBySysId begin.");
	  logger.info("sysId:" + JSON.toJSONString(sysId));
	  List<MenuVo> list = menuMapper.getMenuBylevelAndSysId(0, sysId);// 获取菜单
	  List<MenuVo> menuList=new ArrayList<MenuVo>();
	  //填充一级菜单
	  for (int i=0;i<list.size();) {
		MenuVo vo=list.get(i);
		if(vo.getLevel()==1){
			menuList.add(vo);
			list.remove(i);//删除已经填充项
		}
		else{
			i++;
		}
	  }
	  
	  //填充二级菜单
	  for (MenuVo vo : menuList) {
		 List<MenuVo> mList=new ArrayList<MenuVo>();
		for(int i=0;i<list.size();){
			MenuVo mVo=list.get(i);
			if(vo.getId().equals(mVo.getParentId())){
				mList.add(mVo);
				list.remove(i);//删除已经填充项
			}
			else{
				i++;
			}
		}
		fillMenu(mList,list);
		vo.setChild(mList);
	  }
    logger.info("getMenuBySysId end.");
    return BaseResult.success(menuList);

  }


 /**
  * 根据Id获取菜单详情
  */
  @Override
  public BaseResult getMenuByMenuId(String menuId) {
	  logger.info("getMenuByMenuId begin.");
	  logger.info("menuId:" + JSON.toJSONString(menuId));
    MenuVo vo = menuMapper.getMenuByMenuId(menuId);
    logger.info("getMenuByMenuId end.");
    return BaseResult.success(vo);
  }

  /**
   * 修改菜单
   */
  @Override
  public BaseResult editMenu(MenuVo menuVo) {
	  logger.info("editMenu begin.");
	  logger.info("MenuVo:" + JSON.toJSONString(menuVo));
   /* int sortCount =
        menuMapper.checkSortNum(menuVo.getParentId(), menuVo.getSortNum(), menuVo.getId());

    if (sortCount == 0) {*/
      int count = menuMapper.editMenu(menuVo);
      logger.info("editMenu end.");
      return BaseResult.success(count);
    /*} else {
      return BaseResult.success(-1);// 排序号重复
    }*/
  }


  /**
   * 根据上级Id获取排序号
   */
  @Override
  public BaseResult getSortNumByParentId(String parentId) {
	  logger.info("getSortNumByParentId begin.");
	  logger.info("parentId:" + JSON.toJSONString(parentId));
    int sortNum = menuMapper.getSortNumByParentId(parentId);
    logger.info("getSortNumByParentId end.");
    return BaseResult.success(sortNum);
  }

  /**
   * 批量删除
   */
  @Override
  public BaseResult delMenuByIdArr(String[] menuIdArr) {
	 logger.info("delMenuByIdArr begin.");
	 logger.info("menuIdArr:" + JSON.toJSONString(menuIdArr));
	List<String> menuList=new ArrayList<String>();
	for (String id : menuIdArr) {
		List<MenuVo> list=menuMapper.getAllMenuByParentId(id);
		for (MenuVo vo : list) {
			menuList.add(vo.getId());
		}
	}
	logger.info("menuList:" + JSON.toJSONString(menuList));
	logger.info("isAllocatedPosts begin.");
	List<String> postList=postMapperDao.isAllocatedPosts(menuList);
	logger.info("postList:" + JSON.toJSONString(postList));
	logger.info("isAllocatedPosts end.");
	if(postList!=null&&postList.size()>0){
		 logger.info("delMenuByIdArr end.");
		return BaseResult.fail("0003", "菜单已分配，无法删除！");// 已分配岗位
	}
	else{
		int count = menuMapper.delMenuByIdArr(menuList.toArray(new String[menuList.size()]));
		 logger.info("delMenuByIdArr end.");
	    return BaseResult.success(count);
	}
  }

  /**
   * 
   * @Title: getMenuByStaffPost
   * @Description: 根据员工岗位和系统id获取菜单

   * @return
   * @date: 2016年4月1日 下午12:31:36
   * @return: BaseResult
   */
  @Override
  public BaseResult getMenuByStaffPost(String staff_id, String sysinfo_id) {
    logger.info("getMenuByStaffPost start");
    logger.info("staff_id:" + staff_id);
    logger.info("staff_id:" + sysinfo_id);

    List<MenuVo> menuVoLv1List = new ArrayList<MenuVo>();
    List<MenuVo> menuVoLv2List = new ArrayList<MenuVo>();
    List<MenuVo> menuVoLv3List = new ArrayList<MenuVo>();

    StaffVo staffVo = new StaffVo();
    staffVo.setStaffId(staff_id);
    List<Staff2PostVo> staff2PostVoList = staffDao.getStaff2Post(staffVo);
    if (staff2PostVoList == null || staff2PostVoList.size() == 0) {
      return BaseResult.success(menuVoLv1List);
    }
    logger.info("staff2PostVoList:" + JSON.toJSONString(staff2PostVoList));

    List<Post2MenuVo> post2MenuList = new ArrayList<Post2MenuVo>();

    for (Staff2PostVo staff2PostVo : staff2PostVoList) {
      PostVo postVo = new PostVo();
      postVo.setId(staff2PostVo.getPostId());
      post2MenuList.addAll(postMapperDao.getPost2MenuListByPostId(postVo));
    }
    if (post2MenuList == null || post2MenuList.size() == 0) {
      return BaseResult.success(menuVoLv1List);
    }
    logger.info("post2MenuList:" + JSON.toJSONString(post2MenuList));
    
    List<String> menuIDLsit = new ArrayList<String>();
    for (Post2MenuVo post2MenuVo : post2MenuList) {
      String menuID = post2MenuVo.getMenuId();
      if (!menuIDLsit.contains(menuID)) {
        menuIDLsit.add(menuID);
      }
    }
    if (menuIDLsit == null || menuIDLsit.size() == 0) {
      return BaseResult.success(menuVoLv1List);
    }
    logger.info("menuIDLsit:" + JSON.toJSONString(menuIDLsit));
    
    List<MenuVo> menuVoList = menuMapper.getMenuByMenuIdSys(menuIDLsit, sysinfo_id);

    if (menuVoList == null || menuVoList.size() == 0) {
      return BaseResult.success(menuVoLv1List);
    }
    logger.info("menuVoList:" + JSON.toJSONString(menuVoList));

    for (int i = 0; i < menuVoList.size(); i++) {
      if (menuVoList.get(i).getLevel() == 1) {// 获取一级菜单
        menuVoLv1List.add(menuVoList.get(i));
      }
      if (menuVoList.get(i).getLevel() == 2) {// 获取二级菜单
        menuVoLv2List.add(menuVoList.get(i));
      }
      if (menuVoList.get(i).getLevel() == 3) {// 获取三级菜单
        menuVoLv3List.add(menuVoList.get(i));
      }
    }

//    for (int i = 0; i < menuVoLv1List.size(); i++) {
//
//      String idLv1 = menuVoLv1List.get(i).getId();
//      List<MenuVo> childLv2 = new ArrayList<MenuVo>();// 获取一级菜单的所有二级子菜单
//      for (int j = 0; j < menuVoLv2List.size(); j++) {
//        if (menuVoLv2List.get(j).getParentId().equals(idLv1)) {
//          childLv2.add(menuVoLv2List.get(j));
//        }
//
//        String idLv2 = menuVoLv2List.get(j).getId();
//        List<MenuVo> childLv3 = new ArrayList<MenuVo>();// 获取二级菜单的所有三级子菜单
//        for (int k = 0; k < menuVoLv3List.size(); k++) {
//          if (menuVoLv3List.get(k).getParentId().equals(idLv2)) {
//            childLv3.add(menuVoLv3List.get(k));
//          }
//        }
//
//        menuVoLv2List.get(j).setChild(childLv3);
//      }
//      menuVoLv1List.get(i).setChild(childLv2);
//    }
    
    for(int j = 0; j < menuVoLv2List.size(); j++){
      menuVoLv2List.get(j).setChild(getAllLevelMenu(menuVoLv3List,menuVoLv2List.get(j).getId()));
    }
    
    for(int j = 0; j < menuVoLv1List.size(); j++){
      menuVoLv1List.get(j).setChild(getAllLevelMenu(menuVoLv2List,menuVoLv1List.get(j).getId()));
    }
    
    

    logger.info("menuList:" + JSON.toJSONString(menuVoLv1List));
    logger.info("getMenuByStaffPost end");
    return BaseResult.success(menuVoLv1List);
  }
  
  private List<MenuVo> getAllLevelMenu(List<MenuVo> menuVoLvList,String idLv){
    List<MenuVo> childLv = new ArrayList<MenuVo>();// 获取一级菜单的所有二级子菜单
    for(int i=0;i<menuVoLvList.size();i++){
      if(menuVoLvList.get(i).getParentId().equals(idLv)){
        childLv.add(menuVoLvList.get(i));
      }
    }
    return childLv;
  }
  
  
  /**
   * 根据系统Id列表获取菜单列表
   */
  @Override
  public BaseResult getMenuBySysId(List<String> sysList){
	  logger.info("getMenuBySysId begin.");
	  logger.info("sysList:" + JSON.toJSONString(sysList));
	  List<MenuVo> sysInfoList=menuMapper.getMenuBySysId(sysList);
	  logger.info("getMenuBySysId end");
	  return BaseResult.success(sysInfoList);
  }
}
